function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if(r != null) {
		return unescape(r[2]);
		//return decodeURI(r[2]); //解决中文乱码问题
	}
}

function myBtnClick(obj){

    if($(obj).attr("isChoosen")){
        $(obj).css("background-color", "rgb(255,255,255)");
        $(obj).css("color", "rgb(0,0,0)");
        $(obj).removeAttr("isChoosen");
    }else{
        $(obj).css("background-color", "rgb(224,224,224)");
        $(obj).css("color", "rgb(48,148,222)");
        $(obj).attr("isChoosen","true");
    }

}

//id计数
var count = 88888;

/*设置*/
var setting = {
	async:{
		enable:false
	},
	edit: {
		enable: true,
		showRemoveBtn: false,
		showRenameBtn: false,
		editNameSelectAll: true,
		drag:{
			isMove:false,
			isCopy:false
		}
	},
	check: {
		enable: false,
		chkDisabledInherit: true
	},
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "pid",
			rootPId: 0
		},
		keep: {
			//始终是叶子节点 isParent :false
			leaf: true,
			//始终是父节点 isParent :true
			parent: true
		}
	},
	view: {
		showIcon: false,
		fontCss: getFont,
		dblClickExpand: dblClickExpand,
		selectedMulti:false,
		autoCancelSelected:false

	},
	callback: {
		onClick: clickEvent
	},
	key: {
		isParent: "parent"
	}
};

/*协议节点关系*/
var myPro = [{id: 1, pid: 0, name: "IEC61850-SV", open: true, "chkDisabled": true, fieldType: "root"},
	{id: 2, pid: 1, name: "Header", fieldType: "fixedField"},
	{id: 21, pid: 2, name: "MAC目的地址", fieldType: "fixedField"},
	{id: 22, pid: 2, name: "MAC源地址", fieldType: "fixedField"},

	{id: 3, pid: 1, name: "Priority tagged", fieldType: "fixedField"},
	{id: 31, pid: 3, name: "TPID", fieldType: "fixedField"},
	{id: 32, pid: 3, name: "TCI", fieldType: "fixedField"},

	{id: 4, pid: 1, name: "Ethertype", fieldType: "fixedField"},

	{id:5,pid:1,name:"Header", fieldType: "fixedField", isParent: true},
	{id:51,pid:5,name:"APPID", fieldType: "fixedField"},
	{id:52,pid:5,name:"Length", fieldType: "fixedField"},
	{id:53,pid:5,name:"Reserved1", fieldType: "fixedField"},
	{id:54,pid:5,name:"Reserved2", fieldType: "fixedField"},

	{id:6,pid:1,name:"APDU", fieldType: "fixedField",pType:"APDU",isParent:true},
	{id:61,pid:6,name:"APDU数据", fieldType: "fixedField"},
	{id:62,pid:6,name:"APDU数目", fieldType: "fixedField"},
	{id:63,pid:6,name:"SVID字符串", fieldType: "fixedField"},
	{id:64,pid:6,name:"DatSet字符串", fieldType: "fixedField"},
	{id:65,pid:6,name:"样本计算器 INT16U", fieldType: "fixedField"},
	{id:66,pid:6,name:"配置版本号 INT32U", fieldType: "fixedField"},
	{id:67,pid:6,name:"刷新时间", fieldType: "fixedField"},
	{id:68,pid:6,name:"同步标志", fieldType: "fixedField"},
	{id:69,pid:6,name:"采样率 INT16U", fieldType: "fixedField"},
	{id:691,pid:6,name:"采样值 T-L", fieldType: "fixedField"},
	{id:692,pid:6,name:"通道", fieldType: "fixedField",isParent:true},

	{id:6921,pid:692,name:"数据部分", fieldType: "fixedField"},
	{id:6922,pid:692,name:"q属性", fieldType: "fixedField"},
	{id:69221,pid:6922,name:"q属性的固定部分", fieldType: "fixedField"},
	{id:69222,pid:6922,name:"q属性的可扩展部分", fieldType: "fixedField",pType:"q属性的可扩展部分"},

	{id:7,pid:1,name:"MAC填充", fieldType: "fixedField",pType:"MAC填充"},
	{id:8,pid:1,name:"MAC计算检验", fieldType: "fixedField"},
];


//节点属性
var treeNodeName="";
var treeNodeDescription="";
var treeNodeFuncType="";
var treeNodeFieldType="";

var treeObj;

$(document).ready(function () {
	loadUserInfo()
	treeObj=$.fn.zTree.init($("#myDemo"),setting,myPro)
});


var newId = getQueryString("id");

/*认证*/
var AuthFalg=false;

/*加密*/
var EncryFalg = false;
/*异常*/
var ExceptionFlag = false;
/*防病毒*/
var DefendFalg = false;

/*重传*/
var ResendFalg=false;

/*完整性校验*/
var IntegrityFalg = false;

function dblClickExpand(treeId, treeNode) {
	return treeNode.level > 0;
}

function getFont(treeId, node) {
	return node.font ? node.font : {};
}

function clickEvent(event, treeId, treeNode,clickFlag) {
	clickFlag=1;
	event.ctrlKey=false;
	event.metaKey=false;


	var treeObj = $.fn.zTree.getZTreeObj("myDemo");
	treeNode.selectedMulti=false;

	if(treeNode.name!=undefined){
		treeNodeName=treeNode.name;
	}else{
		treeNodeName="";
	}

	if(treeNode.description!=undefined){
		treeNodeDescription=treeNode.description;
	}else{
		treeNodeDescription="";
	}

	if(treeNode.funcType!=undefined){
		treeNodeFuncType=treeNode.funcType;
	}else{
		treeNodeFuncType="";
	}

	if(treeNode.fieldType!=undefined){
		treeNodeFieldType=treeNode.fieldType;
	}



	$("#fieldArea").empty();
	var fieldAreaHtml = "  <div class=\"fieldDesign\">\n" +
		"                            <label for=\"fieldName\" class=\"control-label\"><span style=\"color: red;\">*</span>字段名称：</label>\n" +
		"                            <input type=\"text\" name=\"fieldName\" maxlength='20' id=\"fieldName\" value=\""+treeNodeName+"\" placeholder=\"必填项(字数不超过20)\">\n" +
		"                        </div>\n" +
		"                        <div class=\"fieldDesign\">\n" +
		"                            <label for=\"description\" class=\"control-label\">字段说明：</label>\n" +
		"                            <input type=\"text\" name=\"description\" id=\"description\" maxlength='30' value=\""+treeNodeDescription+"\" placeholder=\"选填项(字数不超过30)\">\n" +
		"                        </div>\n" +
		"                        <div class=\"fieldDesign\">\n" +
		"                            <label for=\"funcType\" class=\"control-label\"><span style=\"color: red;\">*</span>功能类型：</label>\n" +
		"                            <select id=\"funcType\">\n" +
		"                                <option value=\"null\" disabled selected id=\"null\" >请选择字段的功能类型</option>\n" +
		"                                <option value=\"encryTag\" id=\"encryTag\">加密标识字段</option>\n" +
		"                                <option value=\"encryAlgField\" id=\"encryAlgField\">加密方法字段</option>\n" +
		"                                <option value=\"encryAreaField\" id=\"encryAreaField\">加密区域字段</option>\n" +
		"                                <option value=\"encryModel\" id=\"encryModel\">加密模式字段</option>\n" +
		"                                <option value=\"encryOutputField\" id=\"encryOutputField\">加密输出字段</option>\n" +
		"                                <option value=\"encryResField\" id=\"encryResField\">加密结果字段</option>\n" +
		"                                <option value=\"virusTag\" id=\"virusTag\">防病毒标识字段</option>\n" +
		"                                <option value=\"virusMethodField\" id=\"virusMethodField\">防病毒方法字段</option>\n" +
		"                                <option value=\"IntegrityTag\" id=\"IntegrityTag\">完整性标识字段</option>\n" +
		"                                <option value=\"IntegrityValField\" id=\"IntegrityValField\">完整性校验值字段</option>\n" +
		"                                <option value=\"IntegrityMethodField\" id=\"IntegrityMethodField\">完整性校验方法字段</option>\n" +
		"                                <option value=\"IntegrityResField\" id=\"IntegrityResField\">完整性校验结果字段</option>\n" +
		"                                <option value=\"exceptionTag\" id=\"exceptionTag\">异常标识字段</option>\n" +
		"                                <option value=\"exceptionMethodField\" id=\"exceptionMethodField\">异常检测方法字段</option>\n" +
		"                                <option value=\"exceptionValField\" id=\"exceptionValField\">异常检测值字段</option>\n" +
		"                                <option value=\"exceptionResField\" id=\"exceptionResField\">异常检测结果字段</option>\n" +
		"                                <option value=\"replayCheckField\" id=\"replayCheckField\">重放检测标识字段</option>\n" +
		"                                <option value=\"resendCheckField\" id=\"resendCheckField\">重传检测标识字段</option>\n" +
		"                                <option value=\"resendUniqueField\" id=\"resendUniqueField\">重传唯一性标识字段</option>\n" +
		"                                <option value=\"overtimeCheckField\" id=\"overtimeCheckField\">超时检测标识字段</option>\n" +
		"                                <option value=\"communicationResField\" id=\"communicationResField\">通信状态指示位字段</option>\n" +
		"                                <option value=\"timeSignField\" id=\"timeSignField\">时间戳标识字段</option>\n" +
		"                                <option value=\"customFuncField\" id=\"customFuncField\">自定义功能字段</option>\n" +
		"                            </select>\n" +
		"                        </div>\n" +
		"                        <div class=\"fieldDesign\">\n" +
		"                            <label for=\"fieldType\" class=\"control-label\">字段类型：</label>\n" +
		"                            <select id=\"fieldType\" disabled>\n" +
		"                                <option value=\"null\" selected>-----------------------------------</option>\n" +
		"                                <option value=\"fixedField\" id=\"fixedField\">协议固定字段(自动判定)</option>\n" +
		"                                <option value=\"customField\" id=\"customField\">推荐自定义字段(自动判定)</option>\n" +
		"                                <option value=\"userField\" id=\"userField\">用户自定义字段(自动判定)</option>\n" +
		"                            </select>\n" +
		"                        </div>\n" +
		"                        <button class=\"fieldDesignButton\" id=\""+treeNode.id+"fieldBtn\"><i class=\"fa fa-arrow-right fa-1x\"></i> 确认\n" +
		"                        </button>";
	$("#fieldArea").append(fieldAreaHtml);

	switch (treeNodeFuncType) {
		case "encryTag":$("#encryTag").attr("selected", "selected"); break;
		case "encryAlgField":$("#encryAlgField").attr("selected", "selected"); break;
		case "encryAreaField":$("#encryAreaField").attr("selected", "selected"); break;
		case "encryModel":$("#encryModel").attr("selected", "selected"); break;
		case "encryOutputField":$("#encryOutputField").attr("selected", "selected"); break;
		case "encryResField":$("#encryResField").attr("selected", "selected"); break;
		case "virusTag":$("#virusTag").attr("selected", "selected"); break;
		case "virusMethodField":$("#virusMethodField").attr("selected", "selected"); break;
		case "IntegrityTag":$("#IntegrityTag").attr("selected", "selected"); break;
		case "IntegrityValField":$("#IntegrityValField").attr("selected", "selected"); break;
		case "IntegrityResField":$("#IntegrityResField").attr("selected", "selected"); break;
		case "IntegrityMethodField":$("#IntegrityMethodField").attr("selected", "selected"); break;
		case "exceptionTag":$("#exceptionTag").attr("selected", "selected"); break;
		case "exceptionMethodField":$("#exceptionMethodField").attr("selected", "selected"); break;
		case "exceptionValField":$("#exceptionValField").attr("selected", "selected"); break;
		case "exceptionResField":$("#exceptionResField").attr("selected", "selected"); break;
		case "resendUniqueField":$("#resendUniqueField").attr("selected", "selected"); break;
		case "overtimeCheckField":$("#overtimeCheckField").attr("selected", "selected"); break;
		case "communicationResField":$("#communicationResField").attr("selected", "selected"); break;
		case "timeSignField":$("#timeSignField").attr("selected", "selected"); break;
		case "resendCheckField":$("#resendCheckField").attr("selected", "selected"); break;
		case "customFuncField":$("#customFuncField").attr("selected", "selected"); break;
		default: $("#null").attr("selected", "selected");
	}

	if (treeNodeFieldType == "fixedField") {
		$("#fixedField").attr("selected", "selected");
		$("#description").attr("placeholder","协议固定字段,修改请慎重");
	} else if(treeNodeFieldType == "customField") {
		$("#description").attr("placeholder","推荐设计字段");
		$("#customField").attr("selected", "selected");
		$("#fieldType").css("color", "red");
	}else{
		$("#userField").attr("selected", "selected");
		$("#fieldType").css("color", "#9400D3");
		$("#description").attr("placeholder","用户自定义字段");
	}

	treeNodeName=$("#fieldName").val();
	treeNodeDescription=$("#description").val();
	treeNodeFuncType=$("#funcType option:selected").val();

	$("#"+treeNode.id+"fieldBtn").live("click", function () {

		treeNodeName=$("#fieldName").val();
		treeNodeDescription=$("#description").val();
		treeNodeFuncType=$("#funcType option:selected").val();

		if(!IsNotSpecialCharAndNotEmpty(treeNodeName)){
			// alert("字段名称不能为空");
			$("#fieldName").val("");
			$("#fieldName").attr('placeholder',"不能含有特殊字符且不能为空");
			if((treeNodeDescription.trim()!="")&&!IsSpecialChar(treeNodeDescription,"字段描述")){
				//字段说明不能有特殊字符
				$("#description").val("");
				$("#description").attr('placeholder',"不能有特殊字符");
			}
			return false;
		}
		else if((treeNodeDescription.trim()!="")&&!IsSpecialChar(treeNodeDescription,"字段说明")){
			//字段说明不能有特殊字符
			$("#description").val("");
			$("#description").attr('placeholder',"不能有特殊字符");
		}else if(treeNodeName.length>20){
			// alert("字段名称字数不能超过20");
			$("#fieldName").val("");
			$("#fieldName").attr('placeholder',"字段名称字数不能超过20");
			return false;
		} else if(treeNodeDescription.length>30){
			// alert("字段说明字数不能超过30");
			$("#description").val("");
			$("#description").attr('placeholder',"字段说明字数不能超过30");
			return false;
		}else{
			treeNode.name = treeNodeName;
			treeNode.description = treeNodeDescription;
			treeNode.funcType = treeNodeFuncType;
			treeObj.updateNode(treeNode);
		}
	});


}

function identityFunc() {

	if(!AuthFalg){
		alert("认证协商已开启");
		auth={};
	}else {
		alert("认证协商已关闭");
		auth=null;
	}
	AuthFalg=!AuthFalg;
	myBtnClick("#identity");

}

function encrytionFunc() {
	if (!EncryFalg) {
		var treeObj = $.fn.zTree.getZTreeObj("myDemo");
		//根元素
		var root = treeObj.getNodeByParam("pid", "0");
		if (treeObj.getNodeByParam("name", "安全功能层") == null) {
			//添加安全功能层
			treeObj.addNodes(root, 4, {
				id: (++count),
				name: "安全功能层",
				font: {"color": "red"},
				isParent: true,
				fieldType: "customField",
				type:"templatePackage"
			});
		}
		var securityFunc = treeObj.getNodeByParam("name", "安全功能层");
		treeObj.addNodes(securityFunc, 1, {
			id: (++count),
			name: "EPT",
			font: {"color": "red"},
			fieldType: "customField",
			type:"encry",
			funcType:"encryTag"

		});

		if (treeObj.getNodeByParam("name", "安全功能码") == null) {
			//添加用户自定义安全功能码
			treeObj.addNodes(securityFunc, 1, {
				id: (++count),
				name: "安全功能码",
				fieldType: "customField",
				font: {"color": "red"},
				isParent: true,
				type:"templatePackage"
			});
		}
		var userSecurity = treeObj.getNodeByParam("name", "安全功能码");
		treeObj.addNodes(userSecurity, 1, {
			id: (++count),
			fieldType: "customField",
			name: "加密方法",
			font: {"color": "red"},
			type:"encry",
			funcType:"encryAlgField"
		});
		treeObj.addNodes(userSecurity, 1, {
			id: (++count),
			fieldType: "customField",
			name: "加密模式",
			font: {"color": "red"},
			type:"encry",
			funcType:"encryModel"
		});

		if(treeObj.getNodeByParam("name", "自定义编码") == null){
			var asduUnit = treeObj.getNodeByParam("pType", "MAC填充");
			asduUnit.isParent=true;
			treeObj.updateNode(asduUnit);
			treeObj.addNodes(asduUnit, {
				id: (++count),
				name: "自定义编码",
				font: {"color": "red"},
				fieldType: "customField",
				isParent:true,
				type:"templatePackage"
			});

		}
		var userExtendCode = treeObj.getNodeByParam("name", "自定义编码");
		treeObj.addNodes(userExtendCode,1, {
			id: (++count),
			name: "加密输出格式",
			font: {"color": "red"},
			fieldType: "customField",
			type:"encry",
			funcType:"encryOutputField"
		});

		if(treeObj.getNodeByParam("name", "结果域")==null){
			treeObj.addNodes(root, -1,{
				id: (++count),
				name: "结果域",
				font: {"color": "red"},
				type:"templatePackage",
				isParent: true
			});
		}

		var resNode=treeObj.getNodeByParam("name", "结果域");
		treeObj.addNodes(resNode,{
			id: (++count),
			name:"加密结果字段",
			font: {"color": "red"},
			fieldType: "customField",
			type:"encry",
			funcType:"encryResField",
			isParent:false
		});

		encry={};
		EncryFalg = !EncryFalg;
	} else {
		var treeObj = $.fn.zTree.getZTreeObj("myDemo");
		var encryList=treeObj.getNodesByParam("type","encry");

		for(var i=0;i<encryList.length;i++){
			treeObj.removeNode(encryList[i]);
		}

		for(var n=2;n>0;n--){
			var templatePackageList=treeObj.getNodesByParam("type","templatePackage");
			for(var j=0;j<templatePackageList.length;j++){
				if(templatePackageList[j].children.length==0){
					treeObj.removeNode(templatePackageList[j]);
				}
			}
		}
		function filter(node) {
			//过滤函数
			tree.getNodeByParam("pType","MAC填充");
			return (node.fieldType=="fixedField" && node.pType=="MAC填充");
		}
		var tree=$.fn.zTree.getZTreeObj("myDemo");
		if(tree.getNodeByParam("pType","MAC填充").children.length==0){
			tree.expandNode(tree.getNodesByFilter(filter,true),false,true,false);
			var temp=tree.getNodeByParam("pType","MAC填充");
			temp.isParent=false;
			tree.updateNode(temp);
		}


		encry=null;
		EncryFalg = !EncryFalg;
	}
	myBtnClick($("#encryption"));


}

function exceptionFunc() {

	if (!ExceptionFlag) {

		var treeObj = $.fn.zTree.getZTreeObj("myDemo");
		//根元素
		var root = treeObj.getNodeByParam("pid", "0");

		if (treeObj.getNodeByParam("name", "安全功能层") == null) {
			//添加安全功能层
			treeObj.addNodes(root, 4, {
				id: (++count),
				name: "安全功能层",
				font: {"color": "red"},
				isParent: true,
				fieldType: "customField",
				type: "templatePackage"
			});
		}
		//添加异常检测相关节点
		var securityFunc = treeObj.getNodeByParam("name", "安全功能层");
		treeObj.addNodes(securityFunc, 0, {
			id: (++count),
			name: "AD",
			font: {"color": "red"},
			fieldType: "customField",
			type: "exception",
			funcType: "exceptionTag"
		});

		if (treeObj.getNodeByParam("name", "安全功能码") == null) {
			//添加用户自定义安全功能码
			treeObj.addNodes(securityFunc, 1, {
				id: (++count),
				name: "安全功能码",
				font: {"color": "red"},
				isParent: true,
				fieldType: "customField",
				type: "templatePackage",
			});
		}
		var userSecurity = treeObj.getNodeByParam("name", "安全功能码");
		treeObj.addNodes(userSecurity, 1, {
			id: (++count),
			name: "异常检测方法",
			font: {"color": "red"},
			fieldType: "customField",
			type: "exception",
			funcType: "exceptionMethodField"
		});

		if(treeObj.getNodeByParam("name", "自定义编码") == null){
			var asduUnit = treeObj.getNodeByParam("pType", "MAC填充");
			asduUnit.isParent=true;
			treeObj.updateNode(asduUnit);
			treeObj.addNodes(asduUnit, {
				id: (++count),
				name: "自定义编码",
				font: {"color": "red"},
				fieldType: "customField",
				isParent:true,
				type:"templatePackage"
			});

		}

		var userExtendCode = treeObj.getNodeByParam("name", "自定义编码");
		treeObj.addNodes(userExtendCode,1, {
			id: (++count),
			name: "异常检测值",
			font: {"color": "red"},
			fieldType: "customField",
			type: "exception",
			funcType: "exceptionValField"
		});

		if(treeObj.getNodeByParam("name", "结果域")==null){
			treeObj.addNodes(root, -1,{
				id: (++count),
				name: "结果域",
				font: {"color": "red"},
				type:"templatePackage",
				isParent: true
			});
		}

		var resNode=treeObj.getNodeByParam("name", "结果域");
		treeObj.addNodes(resNode,{

			id: (++count),
			name:"异常结果字段",
			font: {"color": "red"},
			fieldType: "customField",
			type:"exception",
			funcType:"exceptionResField",
			isParent:false
		});

		exception = {};
	} else {
		var treeObj = $.fn.zTree.getZTreeObj("myDemo");

		var exceptionList = treeObj.getNodesByParam("type", "exception");
		for (var i = 0; i < exceptionList.length; i++) {
			treeObj.removeNode(exceptionList[i]);
		}

		for (var n = 2; n > 0; n--) {
			var templatePackageList = treeObj.getNodesByParam("type", "templatePackage");
			for (var j = 0; j < templatePackageList.length; j++) {
				if (templatePackageList[j].children.length == 0) {
					treeObj.removeNode(templatePackageList[j]);
				}
			}
		}

		function filter(node) {
			//过滤函数
			tree.getNodeByParam("pType","MAC填充");
			return (node.fieldType=="fixedField" && node.pType=="MAC填充");
		}
		var tree=$.fn.zTree.getZTreeObj("myDemo");


		if(tree.getNodeByParam("pType","MAC填充").children.length==0){
			tree.expandNode(tree.getNodesByFilter(filter,true),false,true,false);
			var temp=tree.getNodeByParam("pType","MAC填充");
			temp.isParent=false;
			tree.updateNode(temp);
		}

		exception = null;

	}
	ExceptionFlag = !ExceptionFlag;
	myBtnClick($("#exception"));
}

function defendFunc() {
	var treeObj = $.fn.zTree.getZTreeObj("myDemo");
	//根元素
	var root = treeObj.getNodeByParam("pid", "0");

	if (!DefendFalg) {
		if(treeObj.getNodeByParam("name", "自定义编码") == null){
			var asduUnit = treeObj.getNodeByParam("pType", "MAC填充");
			asduUnit.isParent=true;
			treeObj.updateNode(asduUnit);
			treeObj.addNodes(asduUnit, {
				id: (++count),
				name: "自定义编码",
				font: {"color": "red"},
				fieldType: "customField",
				isParent:true,
				type:"templatePackage"
			});
		}
		var userExtendCode = treeObj.getNodeByParam("name", "自定义编码");
		treeObj.addNodes(userExtendCode, {
			id: (++count),
			name: "AT",
			font: {"color": "red"},
			fieldType: "customField",
			type:"virus",
			funcType:"virusTag"
		});
		treeObj.addNodes(userExtendCode, {
			id: (++count),
			name: "防病毒方法",
			font: {"color": "red"},
			fieldType: "customField",
			type:"virus",
			funcType:"virusMethodField"
		});
		virus={};
	} else {
		var virusList=treeObj.getNodesByParam("type","virus");

		for(var i=0;i<virusList.length;i++){
			treeObj.removeNode(virusList[i]);
		}

		for(var n=2;n>0;n--){
			var templatePackageList=treeObj.getNodesByParam("type","templatePackage");
			for(var j=0;j<templatePackageList.length;j++){
				if(templatePackageList[j].children.length==0){
					treeObj.removeNode(templatePackageList[j]);
				}
			}
		}
		function filter(node) {
			//过滤函数
			return (node.fieldType=="fixedField" && node.pType=="MAC填充");
		}
		var tree=$.fn.zTree.getZTreeObj("myDemo");
		if(tree.getNodeByParam("pType","MAC填充").children.length==0){
			tree.expandNode(tree.getNodesByFilter(filter,true),false,true,false);
			var temp=tree.getNodeByParam("pType","MAC填充");
			temp.isParent=false;
			tree.updateNode(temp);
		}

		virus=null;
	}
	DefendFalg = !DefendFalg;
	myBtnClick($("#defend"));
}

function resendFunc() {

	var treeObj = $.fn.zTree.getZTreeObj("myDemo");
	//根元素
	var root = treeObj.getNodeByParam("pid", "0");
	if(treeObj.getNodeByParam("name", "结果域")==null){
		treeObj.addNodes(root, -1,{
			id: (++count),
			name: "结果域",
			font: {"color": "red"},
			type:"templatePackage",
			isParent: true
		});
	}

	var resNode=treeObj.getNodeByParam("name", "结果域");
	treeObj.addNodes(resNode,-1,{
		id: (++count),
		name: "重放检测标识字段",
		font: {"color": "red"},
		fieldType: "customField",
		type:"overTime",
		pType:"重放检测标志位",
		funcType:"replayCheckField"
	});

	treeObj.addNodes(resNode,-1,{
		id: (++count),
		name: "重传检测标识字段",
		font: {"color": "red"},
		fieldType: "customField",
		type:"overTime",
		pType:"重传检测标志位",
		funcType:"resendCheckField"
	});



	var treeObj = $.fn.zTree.getZTreeObj("myDemo");
	//根元素
	var root = treeObj.getNodeByParam("pid", "0");
	if(!ResendFalg){
		if(treeObj.getNodeByParam("pType","重传唯一性标识字段")==null){
			treeObj.addNodes(root,-1,{
				id: (++count),
				name: "重传唯一性标识字段",
				font: {"color": "red"},
				fieldType: "customField",
				type:"overTime",
				pType:"重传唯一性标识字段",
				funcType:"resendUniqueField"
			});
		}

		if(treeObj.getNodeByParam("pType","超时检测标识字段")==null){
			treeObj.addNodes(root,-1,{
				id: (++count),
				name: "超时检测标识字段",
				font: {"color": "red"},
				fieldType: "customField",
				type:"overTime",
				pType:"超时检测标识字段",
				funcType:"overtimeCheckField"
			});
		}



		if(treeObj.getNodeByParam("pType","通信状态指示位字段")==null){
			treeObj.addNodes(root,-1,{
				id: (++count),
				name: "通信状态指示位字段",
				font: {"color": "red"},
				fieldType: "customField",
				type:"overTime",
				pType:"通信状态指示位字段",
				funcType:"communicationResField"
			});
		}


		if(treeObj.getNodeByParam("pType","时间戳标识字段")==null){
			treeObj.addNodes(root,-1,{
				id: (++count),
				name: "时间戳标识字段",
				font: {"color": "red"},
				fieldType: "customField",
				type:"overTime",
				pType:"时间戳标识字段",
				funcType:"timeSignField"
			});
		}
		resend={};
	}else {
		var overTimeList=treeObj.getNodesByParam("type","overTime");
		for(var i=0;i<overTimeList.length;i++){
			treeObj.removeNode(overTimeList[i]);
		}

		for(var n=2;n>0;n--){
			var templatePackageList=treeObj.getNodesByParam("type","templatePackage");
			for(var j=0;j<templatePackageList.length;j++){
				if(templatePackageList[j].children.length==0){
					treeObj.removeNode(templatePackageList[j]);
				}
			}
		}
		//取消
		resend=null;
	}
	ResendFalg=!ResendFalg;
	myBtnClick("#resend");
}

function integrityFunc() {
	var treeObj = $.fn.zTree.getZTreeObj("myDemo");
	//根元素
	var root = treeObj.getNodeByParam("pid", "0");

	if (!IntegrityFalg) {
		if (treeObj.getNodeByParam("name", "安全功能层") == null) {
			//添加安全功能层
			treeObj.addNodes(root, 4, {
				id: (++count),
				name: "安全功能层",
				font: {"color": "red"},
				isParent: true,
				fieldType: "customField",
				type: "templatePackage"
			});
		}
		var securityFunc = treeObj.getNodeByParam("name", "安全功能层");
		treeObj.addNodes(securityFunc, 1, {
			id: (++count),
			name: "IC",
			font: {"color": "red"},
			fieldType: "customField",
			type:"integrity",
			funcType:"IntegrityTag"
		});

		if (treeObj.getNodeByParam("name", "安全功能码") == null) {
			//添加用户自定义安全功能码
			treeObj.addNodes(securityFunc, 1, {
				id: (++count),
				name: "安全功能码",
				font: {"color": "red"},
				isParent: true,
				fieldType: "customField",
				type:"templatePackage"
			});
		}
		var userSecurity = treeObj.getNodeByParam("name", "安全功能码");
		treeObj.addNodes(userSecurity, 1, {
			id: (++count),
			name: "完整性校验方法",
			font: {"color": "red"},
			fieldType: "customField",
			type:"integrity",
			funcType:"IntegrityMethodField"
		});



		//用户自定义扩展编码
		var qExtend = treeObj.getNodeByParam("pType", "q属性的可扩展部分");
		qExtend.isParent=true;
		treeObj.updateNode(qExtend);
		treeObj.addNodes(qExtend, {
			id: (++count),
			name: "完整性校验值",
			font: {"color": "red"},
			fieldType: "customField",
			type:"integrity",
			funcType:"IntegrityValField"
		});

		if(treeObj.getNodeByParam("name", "结果域")==null){
			treeObj.addNodes(root, -1,{
				id: (++count),
				name: "结果域",
				font: {"color": "red"},
				type:"templatePackage",
				isParent: true
			});
		}
		var resNode=treeObj.getNodeByParam("name", "结果域");
		treeObj.addNodes(resNode,{
			id: (++count),
			name:"完整性校验结果字段",
			font: {"color": "red"},
			fieldType: "customField",
			type:"integrity",
			funcType:"IntegrityResField",
			isParent:false
		});

		integrity={};
	} else {

		var integrityList=treeObj.getNodesByParam("type","integrity");
		for(var i=0;i<integrityList.length;i++){
			treeObj.removeNode(integrityList[i]);
		}

		for(var n=2;n>0;n--){
			var templatePackageList=treeObj.getNodesByParam("type","templatePackage");
			for(var j=0;j<templatePackageList.length;j++){
				if(templatePackageList[j].children.length==0){
					treeObj.removeNode(templatePackageList[j]);
				}
			}
		}

		function filter(node) {
			//过滤函数
			return (node.fieldType=="fixedField" && node.pType=="APDU");
		}
		var tree=$.fn.zTree.getZTreeObj("myDemo");
		tree.expandNode(tree.getNodesByFilter(filter,true),false,true,false);

		var nodesByParam = tree.getNodeByParam("pType","q属性的可扩展部分");
		nodesByParam.isParent=false;
		tree.updateNode(nodesByParam);

		integrity=null;
	}
	IntegrityFalg = !IntegrityFalg;
	myBtnClick($("#integrity"));
}

function addFunc(){
	var addTree=$.fn.zTree.getZTreeObj("myDemo");
	var chooseList = addTree.getSelectedNodes();

	if(chooseList.length==0){
		alert("请选择要增加用户自定义字段的节点");
	}else{
		if($.fn.zTree.getZTreeObj("myDemo").getNodesByParam("fieldType","userField").length>9){
			alert("用户自定义字段已达上限");
		}else{

			var addNode=chooseList[0];
			console.log(addNode);
			var root=addTree.getNodeByParam("pid","0");

			var newNode={
				id:"diy"+$.fn.zTree.getZTreeObj("myDemo").getNodesByParam("fieldType","userField").length,
				name:"用户自定义字段",
				fieldType: "userField",
				font:{'color':'#9400D3'},
				isParent:false,
				funcType:"customFuncField"
			};
			if(addNode.fieldType=="root"){
				addTree.addNodes(root,0,newNode);
			}else{
				addTree.updateNode(addNode);
				addTree.addNodes(addNode.getParentNode(),(addNode.getIndex()+1),newNode);
			}

		}


	}
}

function removeFunc(){
	var remTree=$.fn.zTree.getZTreeObj("myDemo");
	var selectedNodes = remTree.getSelectedNodes();
	if(selectedNodes.length==0 || selectedNodes[0].fieldType!="userField"){
		alert("请选择一个用户自定义字段");
		return;
	}else{
		if(confirm("确认删除" + selectedNodes[0].name + " 吗？")){
			var parentNode=selectedNodes[0].getParentNode();
			remTree.updateNode(parentNode);
			remTree.removeNode(selectedNodes[0]);
		}else{
			return;
		}


	}
}

function checkEditFuncField(){
	var treeObj=$.fn.zTree.getZTreeObj("myDemo");
	var nodesList = treeObj.transformToArray(treeObj.getNodes());
	var flag=false;
	var funcList=new Array();
	for(var i=0;i<nodesList.length;i++){
		var node=nodesList[i]
		if(node.funcType!=undefined){
			var thisFuncType=node.funcType;
			if(thisFuncType!="customFuncField"){

				if(funcList.indexOf(thisFuncType)==-1){
					funcList.push(thisFuncType);
				}else{
					flag=true;
					var tip="";
					switch (thisFuncType) {
						case "encryTag":tip="加密标识字段"; break;
						case "encryAlgField":tip="加密方法字段"; break;
						case "encryAreaField":tip="加密区域字段"; break;
						case "encryModel":tip="加密模式字段"; break;
						case "encryOutputField":tip="加密输出字段"; break;
						case "encryResField":tip="加密结果字段"; break;
						case "virusTag":tip="防病毒标识字段"; break;
						case "virusMethodField":tip="防病毒方法字段"; break;
						case "IntegrityTag":tip="完整性标识字段"; break;
						case "IntegrityValField":tip="完整性校验值字段"; break;
						case "IntegrityResField":tip="完整性校验结果字段"; break;
						case "IntegrityMethodField":tip="完整性校验方法字段"; break;
						case "exceptionTag":tip="异常标识字段"; break;
						case "exceptionMethodField":tip="异常检测方法字段"; break;
						case "exceptionValField":tip="异常检测值字段"; break;
						case "exceptionResField":tip="异常检测结果字段"; break;
						case "resendUniqueField":tip="重传唯一性标识字段"; break;
						case "overtimeCheckField":tip="超时检测标识字段"; break;
						case "communicationResField":tip="通信状态指示位字段"; break;
						case "timeSignField":tip="时间戳标识字段"; break;
						case "replayCheckField":tip="重放检测标识字段"; break;
						case "resendCheckField":tip="重传检测标识字段"; break;
						case "customFuncField":tip="自定义功能字段"; break;
						default: tip="未识别字段";
					}
					alert("字段类型:"+tip+" 重复，请检查后再保存");
					break;
				}
			}
		}
	}
	return flag;

}

//协议对象
var designProtocolObj;
//认证
var auth;
//加密
var encry;
//异常
var exception;
//病毒
var virus;
//重传
var resend;
//完整性
var integrity;
//自定义
var customFields;
//zTree
var zTree;



$.ajax({
	type: "get",
	url: HOST.concat("/protocolDesigns/id"),
	dataType: "json",
	data: {
		"id": newId,
		"token": getCookie("token")
	},
	success: function(datas) {

		checkLogin(datas);
		designProtocolObj = datas.data;
		console.log("-----------");
		console.log(designProtocolObj);
		//认证
		auth = designProtocolObj.auth;
		//加密
		encry = designProtocolObj.encrypt;
		//异常
		exception = designProtocolObj.exception;
		//病毒
		virus = designProtocolObj.virus;
		//重传
		resend = designProtocolObj.resend;
		//完整性
		integrity = designProtocolObj.integrity;
		//自定义
		customFields = designProtocolObj.customField;

		//zTree
		zTree=designProtocolObj.zTree;

		if(zTree!=null&&zTree!=undefined){
			myPro=zTree.nodes;
			console.log(zTree.nodes);

			//setting=zTree.setting;
			nodes=zTree.nodes;
			treeObj=$.fn.zTree.init($("#myDemo"),setting,treeObj.transformTozTreeNodes(nodes));
			treeObj.reAsyncChildNodes(treeObj.getNodesByParam("pid","0"), "refresh");

			//id计数
			count = treeObj.transformToArray(treeObj.getNodes()).length;

			$("#identity").bind("click", identityFunc);
			$("#encryption").bind("click", encrytionFunc);
			$("#exception").bind("click", exceptionFunc);
			$("#defend").bind("click", defendFunc);
			$("#resend").bind("click", resendFunc);
			$("#integrity").bind("click", integrityFunc);

			$("#addBtn").bind("click", addFunc);
			$("#removeBtn").bind("click", removeFunc);

			var fieldTypeList= treeObj.getNodesByParam("fieldType","customField");
			for(var i=0;i<fieldTypeList.length;i++){
				treeObj.setting.view.fontCss.color="red";
				treeObj.updateNode(fieldTypeList[i]);
			}

		}

		if(auth!=null&&auth!=undefined){
			AuthFalg=true;
			auth={};
			myBtnClick($("#identity"));
		}

		if(encry!=null&&encry!=undefined){
			EncryFalg=true;
			encry={};
			myBtnClick($("#encryption"));
		}

		if(exception!=null&&exception!=undefined){
			exception={};
			ExceptionFlag=true;
			myBtnClick($("#exception"));
		}

		if(virus!=null&&virus!=undefined){
			virus={};
			DefendFalg=true;
			myBtnClick($("#defend"));
		}

		if(resend!=null&&resend!=undefined){
			resend={};
			ResendFalg=true;
			myBtnClick($("#resend"));
		}

		if(integrity!=null&&integrity!=undefined){
			integrity={};
			IntegrityFalg=true;
			myBtnClick($("#integrity"));
		}
	}
}); //endfor ajax


$("#saveEditon").live("click",function() {

	if(!checkEditFuncField()){
		//js 协议对象
		var detailObj = {
			"id":newId,
			"protocolName": designProtocolObj.protocolName,
			"designName": designProtocolObj.designName,
		};

		if(auth!=null&&auth!=undefined){
			detailObj.auth=auth;
		}

		if(encry!=null&&encry!=undefined){
			detailObj.encrypt=encry;
		}

		if(exception!=null&&exception!=undefined){
			detailObj.exception=exception;
		}

		if(virus!=null&&virus!=undefined){
			detailObj.virus=virus;
		}

		if(resend!=null&&resend!=undefined){
			detailObj.resend=resend;
		}

		if(integrity!=null&&integrity!=undefined){
			detailObj.integrity=integrity;
		}

		if(zTree!=null&&zTree!=undefined){
			var tempObj=$.fn.zTree.getZTreeObj("myDemo");
			detailObj.zTree={
				setting:setting,
				//nodes:treeObj.getNodes()
				nodes:tempObj.getNodes()
			};

		}

		if(auth==null&&encry==null&&exception==null&&virus==null&&resend==null&&integrity==null){
			alert("请选择设计功能");
		}else{
			//可以发送
			$.ajax({
				url:HOST.concat("/protocolDesigns/id?token=")+config.token,
				type:"put",
				dataType:"json",
				contentType:"application/json",
				data:JSON.stringify(detailObj),
				success:function(res){
					checkLogin(res);
					alert("保存成功");
					window.location.href="designProtocol.html";
				},
				error:function(){
					alert("修改失败!");
				}
			});

		}
	}




})


